package com.siyougame.web.controller.system;


import com.siyougame.common.constant.Constants;
import com.siyougame.common.core.domain.AjaxResult;
import com.siyougame.common.core.domain.entity.SysMenu;
import com.siyougame.common.core.domain.entity.SysUser;
import com.siyougame.common.core.domain.model.LoginBody;
import com.siyougame.common.utils.SecurityUtils;
import com.siyougame.framework.web.service.SysLoginService;
import com.siyougame.framework.web.service.SysPermissionService;
import com.siyougame.system.service.ISysDictDataService;
import com.siyougame.system.service.ISysDictTypeService;
import com.siyougame.system.service.ISysMenuService;
import com.siyougame.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
 * 登录验证
 *
 * @author scrm
 */
@Slf4j
@RestController
public class SysLoginController {
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private WxMpService wxMpService;
    private WxCpService wxCpService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ISysDictDataService dictDataService;
//    @Autowired
//    private SysUserApiService sysUserApiService;

    @Autowired
    private ISysDictTypeService dictTypeService;
    @Autowired
    private ISysUserService userService;



    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(),loginBody.getCode(),loginBody.getUuid(), loginBody.getAjCode());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters() {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }

    // ========== 社交登录相关 ==========


    /**
     * 企业微信社交登录授权地址获取
     * @param type
     * @param redirectUri
     * @param response
     * @return
     * @throws IOException
     */
    @GetMapping("/social-login/getAuthUrl")
    public AjaxResult socialLogin(@RequestParam("redirect_uri") String redirectUri, HttpServletResponse response) throws IOException {
        String authorizationUrl = wxMpService.getOAuth2Service().buildAuthorizationUrl(redirectUri, "snsapi_userinfo", "snsapi_userinfo");;
        return AjaxResult.success(authorizationUrl);
    }


    // @RequestMapping("/social-login/back")
    // public AjaxResult login( AuthCallback callback) throws WxErrorException {
    //
    //     WxOAuth2AccessToken accessToken = wxMpService.getOAuth2Service().getAccessToken(callback.getCode());
    //     WxOAuth2UserInfo wxOAuth2UserInfo = wxMpService.getOAuth2Service().getUserInfo(accessToken, "zh_CN");
    //
    //     WxUserDTO wxUser = apiWxUserService.getWxUserByOpenId(wxOAuth2UserInfo.getOpenid());
    //     if (wxUser == null){
    //         wxUser = new WxUserDTO();
    //         wxUser.setGzhOpenid(wxOAuth2UserInfo.getOpenid());
    //         wxUser.setNickname(wxOAuth2UserInfo.getNickname());
    //         wxUser.setHeadimgurl(wxOAuth2UserInfo.getHeadImgUrl());
    //        apiWxUserService.createWxUser(wxUser);
    //     }
    //     //SysUser sysUser = userService.selectUserById(userId);
    //     AjaxResult ajax = AjaxResult.success();
    //     // 生成令牌
    //     StpUtil.login(wxUser.getId());
    //
    //     return ajax;
    // }
}
